package com.sdu.shanyiapi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.sdu.shanyiapi.common.R;
import com.sdu.shanyiapi.controller.form.InsertOrUpdateUserInfoForm;
import com.sdu.shanyiapi.db.pojo.UserInfoEntity;
import com.sdu.shanyiapi.service.UserInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;

/**
 * @author lihaopeng
 * @version 1.0
 * @project shanyi-api
 * @description 用户信息
 * @since 2024/4/21 18:19
 */
@RestController
@RequestMapping("/user/info")
public class UserInfoController {
    @Resource
    public UserInfoService userInfoService;

    @PostMapping("/insertUserInfo")
    @SaCheckLogin
    public R insertUserInfo(@RequestBody @Valid InsertOrUpdateUserInfoForm form) {
        UserInfoEntity entity = BeanUtil.toBean(form, UserInfoEntity.class);
        int userId = StpUtil.getLoginIdAsInt();
        entity.setUserId(userId);
        entity.setUuid(IdUtil.simpleUUID());

        // 计算bmi
        double height = Double.parseDouble(entity.getHeight());
        double weight = Double.parseDouble(entity.getWeight());
        double bmi = weight / (height * height);
        // 保留两位小数
        bmi = (double) Math.round(bmi * 100) / 100;
        entity.setBmi(bmi + "");

        String json = JSONUtil.parseArray(form.getLikes()).toString();
        entity.setLikes(json);
        json = JSONUtil.parseArray(form.getDislikes()).toString();
        entity.setDislikes(json);
        json = JSONUtil.parseArray(form.getDiseases()).toString();
        entity.setDiseases(json);

        userInfoService.insertUserInfo(entity);
        return R.ok();
    }

    @GetMapping("/searchUserInfo")
    @SaCheckLogin
    public R searchUserInfo() {
        int userId = StpUtil.getLoginIdAsInt();
        HashMap map = userInfoService.searchUserInfo(userId);
        if (MapUtil.isEmpty(map)) {
            return R.ok("没有查询到数据");
        }
        return R.ok(map);
    }

    @PostMapping("/updateUserInfo")
    @SaCheckLogin
    public R updateUserInfo(@RequestBody @Valid InsertOrUpdateUserInfoForm form) {
        UserInfoEntity entity = BeanUtil.toBean(form, UserInfoEntity.class);

        // 计算bmi
        double height = Double.parseDouble(entity.getHeight());
        double weight = Double.parseDouble(entity.getWeight());
        double bmi = weight / (height * height);
        // 保留两位小数
        bmi = (double) Math.round(bmi * 100) / 100;
        entity.setBmi(bmi + "");

        String json = JSONUtil.parseArray(form.getLikes()).toString();
        entity.setLikes(json);
        json = JSONUtil.parseArray(form.getDislikes()).toString();
        entity.setDislikes(json);
        json = JSONUtil.parseArray(form.getDiseases()).toString();
        entity.setDiseases(json);

        userInfoService.updateUserInfo(entity);
        return R.ok();
    }
}
